package apkg;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import java.util.*;

import javax.imageio.ImageIO;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

public class ExampleClient {

	public static void main (String[] args) throws Exception {

		QName qName = new QName("http://apkg/", "TestWebServiceService");
		URL url = new URL("http://localhost:9080/TestGlassfish/TestWebServiceService?wsdl");
		Service service = Service.create(url, qName);
		System.out.println("service created");
		TestWebServiceInterface myService = service.getPort(new QName("http://apkg/", "TestWebServicePort"), TestWebServiceInterface.class);

		//enable MTOM in client
		BindingProvider bp = (BindingProvider) myService;
		SOAPBinding binding = (SOAPBinding) bp.getBinding();
		bp.getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY,true);
		binding.setMTOMEnabled(true);

		System.out.println("get image from web");
		URL imageUrl = new URL("http://www.ua.ac.be/images/ua/container3730/logo_UA_hor_kl.jpg");
		Image image = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(imageUrl);

		System.out.println("upload image to service: " + myService.uploadImage("test.png", image));

		System.out.println("got " + myService.sayHello("niel")); 
		
		myService.setup("http://localhost:8080/", "jini://localhost", "niel", "deckx", 230);
		
		myService.registerWithBank();
		
		myService.credit(3000);
			
		myService.offerProduct("Macbook", 20*1000, 300, 1200, "be.ac.ua.codebase.VickreyAuction");
		
		String productId = myService.search("Macbook", 0);

		myService.joinBidding(productId);
		
		myService.placeBidding(productId, 100);
		
		myService.placeBidding(productId, 600);
		

		image = myService.downloadImage("test.png");
		if (image != null) {
			System.out.println("image downloaded");
			String name = "testdownload.png";
			String currentDir = new File("").getAbsolutePath();
			File file = new File(currentDir + "/" + name);

			int mid = name.lastIndexOf(".");
			String format = name.substring(mid+1,name.length());

			System.out.println("format " + format);

			file.createNewFile();

			System.out.println("write file to : " + file.toString());

			ImageIO.write((BufferedImage)image, format, file);
		} else {
			System.out.println("image download failed");
		}


	}
}